<template>
	<view>
		<u-navbar :title="title" :autoBack="true" placeholder>
		</u-navbar>
		<view class="content-container">
			<view v-if="loading" class="loading-container">
				<u-loading-icon mode="flower"></u-loading-icon>
				<text class="loading-text">加载中...</text>
			</view>
			<view v-else-if="error" class="error-container">
				<text class="error-text">{{ error }}</text>
				<u-button text="重新加载" @click="loadDocument"
					customStyle="margin-top: 20px; background-color: #24C98F; color: #fff;"></u-button>
			</view>
			<view v-else class="document-content">
				<view class="document-title">{{ documentData.title }}</view>
				<!-- <view class="document-body" v-html="documentData.content"></view> -->
				<rich-text :nodes="formatRichText(documentData.content)"></rich-text>
				<view class="document-footer">
					<text class="update-time">更新时间：{{ formatTime(documentData.updateTime) }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				documentType: null, // 文档类型：1-隐私条款，4-服务协议
				loading: false,
				error: '',
				documentData: {
					title: '',
					content: '',
					updateTime: ''
				}
			}
		},
		onLoad(options) {
			this.title = options.title || '文档详情';
			this.documentType = parseInt(options.type) || null;
			// 根据类型设置默认标题
			if (!options.title) {
				if (this.documentType === 1) {
					this.title = '隐私条款';
				} else if (this.documentType === 4) {
					this.title = '服务协议';
				}
			}

			// 加载文档内容
			if (this.documentType) {
				this.loadDocument();
			} else {
				this.error = '缺少文档类型参数';
			}
		},
		methods: {
			// 加载文档内容
			loadDocument() {
				if (!this.documentType) {
					this.error = '缺少文档类型参数';
					return;
				}

				this.loading = true;
				this.error = '';

				this.$api.get({
					url: '/companion/updateInfo/service-agreement',
					data: {
						id: this.documentType
					},
					success: (res) => {
						this.loading = false;
						if (res) {
							this.documentData = {
								title: res.title,
								content: res.content || '',
								updateTime: res.updateTime || res.createTime || ''
							};
						} else {
							this.error = '文档内容为空';
						}
					},
					fail: (err) => {
						this.loading = false;
						console.error('加载文档失败:', err);

						let errorMsg = '加载失败，请稍后重试';
						if (err.data && err.data.msg) {
							errorMsg = err.data.msg;
						} else if (err.msg) {
							errorMsg = err.msg;
						}

						this.error = errorMsg;
					}
				});
			},

			// 格式化时间
			formatTime(timeStr) {
				if (!timeStr) return '';

				try {
					const date = new Date(timeStr);
					const year = date.getFullYear();
					const month = String(date.getMonth() + 1).padStart(2, '0');
					const day = String(date.getDate()).padStart(2, '0');
					const hours = String(date.getHours()).padStart(2, '0');
					const minutes = String(date.getMinutes()).padStart(2, '0');

					return `${year}-${month}-${day} ${hours}:${minutes}`;
				} catch (error) {
					console.error('时间格式化失败:', error);
					return timeStr;
				}
			}
		}
	}
</script>

<style lang="scss">
page {
	background-color: #F5F5F5;
}

.content-container {
	padding: 16rpx;
	min-height: calc(100vh - 88rpx);
}

.loading-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 100rpx 0;

	.loading-text {
		margin-top: 20rpx;
		font-size: 28rpx;
		color: #666;
	}
}

.error-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 100rpx 32rpx;

	.error-text {
		font-size: 28rpx;
		color: #999;
		text-align: center;
		line-height: 1.6;
	}
}

.document-content {
	background-color: #fff;
	border-radius: 16rpx;
	padding: 32rpx;
	margin-bottom: 32rpx;

	.document-title {
		font-size: 36rpx;
		font-weight: 600;
		color: #333;
		text-align: center;
		margin-bottom: 32rpx;
		line-height: 1.4;
	}

	.document-body {
		font-size: 28rpx;
		color: #666;
		line-height: 1.8;
		text-align: justify;

		// 富文本内容样式
		::v-deep p {
			margin-bottom: 16rpx;
			text-indent: 2em;
		}

		::v-deep h1, ::v-deep h2, ::v-deep h3, ::v-deep h4, ::v-deep h5, ::v-deep h6 {
			font-weight: 600;
			color: #333;
			margin: 32rpx 0 16rpx 0;
		}

		::v-deep h1 { font-size: 32rpx; }
		::v-deep h2 { font-size: 30rpx; }
		::v-deep h3 { font-size: 28rpx; }

		::v-deep ul, ::v-deep ol {
			padding-left: 32rpx;
			margin-bottom: 16rpx;
		}

		::v-deep li {
			margin-bottom: 8rpx;
		}

		::v-deep strong, ::v-deep b {
			font-weight: 600;
			color: #333;
		}

		::v-deep a {
			color: #24C98F;
			text-decoration: underline;
		}
	}

	.document-footer {
		margin-top: 48rpx;
		padding-top: 32rpx;
		border-top: 1px solid #f0f0f0;

		.update-time {
			font-size: 24rpx;
			color: #999;
			text-align: center;
			display: block;
		}
	}
}
</style>
